<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */

namespace App\Api\Controllers\Auths;

use App\Api\BaseApi;
use App\Models\DividendAccount;
use App\Models\DividendTransaction;
use App\Service\Setting\BeanSettingService;
use App\Service\User\BeanAccountService;
use App\Service\User\DividendAccountServices;
use App\Service\User\PointAccountServices;
use App\Service\User\SinglePoolService;

class DividendApi extends BaseApi
{
    public function InjectIntegralPool() //注入积分池
    {
        $request = self::validate([
            'amount' => 'required|integer',
        ]);
        $beanSetting = BeanSettingService::make();
        if ($beanSetting->getSetting('OPEN_INJECT_INTEGRAL_POOL') == 0){
            return responses('暂未开启转入分红功能！',7);
        }
        $user = self::UserModel();
        $inject_integral = $user->inject_integral ?? 0;
        $this->transaction_password($user);
        $amount = $request->get('amount');
        if ($amount > $inject_integral) {
            return responses('超出转入上限:' . $inject_integral, 7);
        }
        if ($amount % 1000 != 0) {
            return responses('必须为1000的倍数', 7);
        }

        $handlingFeeSetting = $beanSetting->getSetting('POINT_HANDLING_FEE');
        if ($handlingFeeSetting > 0) {
            $handlingFee = bcmul($amount, $handlingFeeSetting, 2);
            $msg = sprintf('充值到积分分红账户 手续费:%s 入帐:%s', $handlingFee, $amount);
            $pa = BeanAccountService::make()
                ->consumption(
                    $user->id,
                    $handlingFee,
                    $msg,
                    5
                );
            if (!$pa) {
                return responses('通证不足，操作失败', 7);
            }
            BeanSettingService::make()->increaseTheAmountOfCombustion($handlingFee, $msg);
        }

        $userId = $user->id;
        $pa = PointAccountServices::make()
            ->consumption($userId, $amount, '充值到积分分红账户', 3);
        if (!$pa) {
            return responses('积分不足，操作失败', 7);
        }
        SinglePoolService::make()->recharge($userId, $amount);
        $user->decrement('inject_integral',$amount);
        return responses('操作成功');
    }

    public function _InjectBeanPool() //注入通证池
    {
        $request = self::validate([
            'amount' => 'required|integer',
        ]);
        $user = self::UserModel();
        $this->transaction_password($user);
        $_amount = $request->get('amount');
        if ($_amount % 1000 != 0) {
            return responses('必须为1000的倍数', 7);
        }
        $beanSetting = BeanSettingService::make();
        $beanPrice = $beanSetting->getSetting('PRICE');
        $handlingFeeSetting = $beanSetting->getSetting('BEAN_HANDLING_FEE');
        $amount = bcdiv($_amount, $beanPrice, 2);
        if ($handlingFeeSetting > 0) {
            $handlingFee = bcmul($_amount, $handlingFeeSetting, 2);
            $TotalAmount = bcadd($_amount, $handlingFee, 2);
            $msg = sprintf('充值到通证分红账户 手续费:%s 入帐:%s 当日价:%s 到账:%s', $handlingFee, $_amount, $beanPrice, $amount);
        } else {
            $TotalAmount = $amount;
            $msg = sprintf('充值到通证分红账户 入帐:%s 当日价:%s 到账:%s', $_amount, $beanPrice, $amount);
        }
        $userId = self::UserId();
        $pa = BeanAccountService::make()
            ->consumption(
                $userId,
                $TotalAmount,
                $msg,
                5
            );
        if (!$pa) {
            return responses('通证不足，操作失败', 7);
        }
        DividendAccountServices::make()->recharge($userId, $amount, 2);
        return responses('操作成功');
    }

    public function getMyDividerAccount()
    {
        $res = DividendAccount::query()
            ->where('user_id', self::UserId())
            ->first();
        return responses($res);
    }

    public function getMyDividerAccountTransaction()
    {
        $type = request('type', false);
        $res = DividendTransaction::query()
            ->when($type, function ($query, $type) {
                return $query->where('type', $type);
            })
            ->where('user_id', self::UserId())
            ->latest('id')
            ->paginate(20);
        return responses($res);
    }
}
